"""yml格式文件操作"""
import os
import yaml
from constant import test_data_path
from utils.utils_log import loggerCase
from utils.utils_json import read_json
from constant import json_data


def read_yaml(file_name):
    """
    读取yaml文件
    """
    file_path = test_data_path
    with open(os.path.join(file_path, file_name) + '.yml', 'r', encoding='utf-8') as f:
        data = yaml.load(stream=f, Loader=yaml.FullLoader)
    return data


class ScreeningYaml:
    """
    处理和筛选yaml文件
    """

    def __init__(self, yaml_name):
        """
        加载yaml数据
        读取用例筛选规则
        """
        self.yaml_data = read_yaml(yaml_name)
        self.screening_conditions_path = json_data + '/screening_conditions.json'
        self.screening_test_case = read_json(self.screening_conditions_path)
        self.title = self.screening_test_case['case_title']
        self.tb_level = self.screening_test_case['tb_level']
        if self.tb_level:
            self.tb_level = self.tb_level.split('-')
        self.tb_type = self.screening_test_case['tb_type']
        if self.tb_type:
            self.tb_type = self.tb_type.split('-')
        loggerCase.info(f'用例显示规则为：case_title: {self.title}, level: {self.tb_level}, type: {self.tb_type}')

    def __call__(self, *args, **kwargs):
        """
        返回处理好的测试数据
        """
        # 整理符合要求的测试用例title
        if self.title and not self.tb_type and not self.tb_level:
            # 只有title要求
            test_case_list = self.screening_title()
        elif not self.title and not self.tb_type and self.tb_level:
            # 只有tb_type要求
            test_case_list = self.screening_other('tb_level', self.tb_level)
        elif not self.title and self.tb_type and not self.tb_level:
            # 只有tb_level要求
            test_case_list = self.screening_other('tb_type', self.tb_type)
        elif not self.title and not self.tb_type and not self.tb_level:
            # 全部都是空
            test_case_list = self.screening_all()
        else:
            # 同过条件组合进行筛选
            test_case_list = self.combination_screening()
        # 通过title返回整体的测试用例数据
        test_case_data_list = []
        for data in self.yaml_data:
            if data['title'] in test_case_list:
                test_case_data_list.append(data)
        loggerCase.info(f'本次执行用例title：{test_case_data_list}')
        return test_case_data_list

    def screening_title(self):
        """
        筛选符合包含对应title内容的title
        """
        title_list = []
        for yaml_data in self.yaml_data:
            if self.title in yaml_data['title']:
                title_list.append(yaml_data['title'])
        if title_list:
            return title_list
        else:
            return []

    def screening_other(self, screening_type, screening_value):
        """
        筛选符合包含对应用例的title
        """
        tb_screening_list = []
        for yaml_data in self.yaml_data:
            if yaml_data['other'][screening_type] in screening_value:
                tb_screening_list.append(yaml_data['title'])
        if tb_screening_list:
            return tb_screening_list
        else:
            return []

    def screening_all(self):
        """
        筛选所有用例
        """
        all_test_list = []
        for yaml_data in self.yaml_data:
            all_test_list.append(yaml_data['title'])
        if all_test_list:
            return all_test_list
        else:
            return []

    def combination_screening(self):
        """
        筛选需要执行的测试用例的title
        """

        # 获取用例
        test_case_list = [[] for i in range(3)]
        for data in self.yaml_data:
            if self.title:
                if self.title in data['title']:
                    test_case_list[0].append(data['title'])
            if self.tb_level:
                if data['other']['tb_level'] in self.tb_level:
                    test_case_list[1].append(data['title'])
            if self.tb_type:
                if data['other']['tb_type'] in self.tb_type:
                    test_case_list[2].append(data['title'])

        # 删除空的list
        for i in range(len(test_case_list) - 1):
            if not test_case_list[i]:
                test_case_list.pop(i)

        # 将相同的list合并
        if len(test_case_list) == 1:
            return test_case_list[0]
        elif len(test_case_list) >= 2:
            test_case_set = set(test_case_list[0])
            for index_list in test_case_list[1:]:
                test_case_list = test_case_set.intersection(index_list)
                return list(test_case_list)
        else:
            return test_case_list

    def filter_title(self, title):
        """
        筛选符合包含对应title内容的title
        """
        title_list = []
        for data in self.yaml_data:
            if title in data['title']:
                title_list.append(data)
        if title_list:
            return title_list
        else:
            return []


if __name__ == '__main__':
    print(ScreeningYaml('scan_grid_data').filter_title("验证P3文件导入"))
